//index.js
//获取应用实例
const util = require('../../utils/util.js')
const app = getApp()


Page({

  /**
   * 页面的初始数据
   * type=&page=1&count=6&tbl=minjianbaitai&ids=
   */
  data: {
    newList: {},
    close: false,
    param: {
      'type': 1,
      'page': 1,
      'size': 6
    },
     is_go_app: 0, //是否可以跳转会APP,0否 1是
     is_into_app:false, //是否是APP进入
      promote_time:'',
      second:120, //倒计时多少秒设置
      second_num:0,  //倒计时调用次数
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var test=wx.cloud.init;
    const db = wx.cloud.database()
    return false
    db.collection('books').where({
      publishInfo: {
        country: 'United States'
      }
    }).get({
      success: function (res) {
        // 输出 [{ "title": "The Catcher in the Rye", ... }]
        console.log(res)
      }
    })
    return false
    console.log(1111)
    if (app.globalData.is_into_app == true && this.data.second_num==0){
      //APP进入小程序
      console.log(app.globalData.is_into_app)
      // var uninid=options.uninid;
      // console.log(options,'数据')
      this.timeCountdown(this.data.second)
      this.setData({
        is_into_app:true,
        second_num:1
      })
    }
  //随机取数
    wx.showNavigationBarLoading();
    // var rand_type = parseInt(Math.random() * 20 + 1);
    var rand_page = parseInt(Math.random() * 20 + 1)
      var rand_param = {
        'type': 1,
        'page': rand_page,
        'size': 6
      } 
      this.setData({
        param: rand_param
      })
      var url = app.url.wxarticle;
      util.http(url, this.newResponse, this.data.param)
  },

  //时间倒计时
  // timeCountdown:function(that) {
  //   var second = that.data.second
  //   var second = that.data.second
  //   if (second == 0) {
  //     that.setData({
  //       is_go_app: 1
  //     });
  //     return;
  //   }
  //   var time = setTimeout(function () {
  //     console.log(second)
  //     that.setData({
  //       second: second - 1
  //     });
  //     that.timeCountdown(that);
  //   }
  //     , 1000)
  // },
  //时间倒计时
  timeCountdown: function (time_difference) {
    if (parseInt(time_difference) == 0) {
      this.setData({
        is_promote: 0,
        is_go_app:1,
      })
    } else {
      var that = this;
      if (app.globalData.is_into_app == true) {
        var int = setTimeout(function () {
          var t = time_difference--;
          var d = Math.floor(t / 60 / 60 / 24);
          var h = Math.floor((t - d * 60 * 60 * 24) / 60 / 60);
          var m = Math.floor((t - h * 60 * 60 - d * 60 * 60 * 24) / 60);
          var s = t;
          if (h < 10) h = "0" + h;
          if (m < 10) m = "0" + m;
          if (s < 10) s = "0" + s;
          var promote_time = {
            "d": d,
            "h": h,
            "m": m,
            "s": s
          };
          console.log(promote_time.s)
          that.setData({
            promote_time: promote_time,
            is_promote: 1,
            is_go_app: 0,
            int: int,
          })
          if (parseInt(t) == 0 && parseInt(d) == 0 && parseInt(h) == 0 && parseInt(m) == 0 && parseInt(s) == 0) {
            console.log(s, '停止')
            that.setData({
              is_promote: 0,
              is_go_app: 1,
            })
            clearInterval(that.data.int);
          }
          that.timeCountdown(time_difference);
        }, 1000)
      }
    
    }
    // setTimeout(function () { wx.hideNavigationBarLoading(); }, 1000);
  },
  //打开APP
  launchAppError: function (e) {
     console.log(e,'返回错误')
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if(app.globalData.is_into_app == true){
      this.setData({
        is_into_app:true, 
        is_show: true
        
      })
    }else{
      this.setData({
        is_into_app: false,
        is_show: true,
      })
    }
    app.is_duanzi = false
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    
    /*
    let that = this;
    var url = app.url.wxarticle;
    var rand_param = {
      'type': this.data.param.type,
      'page': this.data.param.page+1,
      'size': this.data.param.size
    }
    this.setData({
      param: rand_param
    })
    util.http(url, function(res) {
      console.log(res.data);
      let newList = res.data
      that.setData({
        newList: newList
      })
      wx.stopPullDownRefresh()
    }, rand_param)
    */
    wx.showNavigationBarLoading();
    this.setData({
      newList: {},
    })
    // var rand_type = parseInt(Math.random() * 15 + 1);
    var rand_page = parseInt(Math.random() * 15 + 1)
    var rand_param = {
      'type': 1,
      'page': rand_page,
      'size': 6
    }
    this.setData({
      param: rand_param
    })
    var url = app.url.wxarticle;
    util.http(url, this.newResponse, this.data.param)
   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.data.param.page += 1;
    var url = app.url.wxarticle;
    util.http(url, this.newResponse, this.data.param)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  updateNews: function() {
    wx.showNavigationBarLoading();
    this.setData({
      newList: {},
    })
    // var rand_type = parseInt(Math.random() * 15 + 1);
    var rand_page = parseInt(Math.random() * 15 + 1)
    var rand_param = {
      'type': 1,
      'page': rand_page,
      'size': 6
    }
    this.setData({
      param: rand_param
    })
    var url = app.url.wxarticle;
    util.http(url, this.newResponse, this.data.param)
  },

  newResponse: function(res) {
    console.log(res,'1')
    let newList = res.data.list
    if(newList==null){
      this.onLoad()
      return false
    }
    let f_newList = {};
    if (this.data.newList.length > 0) {
      f_newList = this.data.newList.concat(newList)
    } else {
      f_newList = newList;
    }
    this.setData({
      newList: f_newList,
    })
    setTimeout(function () { wx.hideNavigationBarLoading(); }, 1000);
  },

  closeUpdate: function(event) {
    this.setData({
      close: true,
    })

    console.log(this.data.close)

  },
  onArticelDetail: function(event) {
    console.log(event);
    let id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../articel-detail/articel-detail?id=' + id,
    })
    console.log(id);
  },
  rotateAndScale: function() {
    // 旋转同时放大
    this.animation.rotate(45).scale(2, 2).step()
    this.setData({
      animationData: this.animation.export()
    })
  },
  rotateThenScale: function() {
    // 先旋转后放大
    this.animation.rotate(45).step()
    this.animation.scale(2, 2).step()
    this.setData({
      animationData: this.animation.export()
    })
  },
  rotateAndScaleThenTranslate: function() {
    // 先旋转同时放大，然后平移
    this.animation.rotate(45).scale(2, 2).step()
    this.animation.translate(100, 100).step({
      duration: 1000
    })
    this.setData({
      animationData: this.animation.export()
    })
  }
})
